import { createStore } from "vuex"

 //购物车模块
const moduleCart = {
    //()=>( {aa:bb} ) === ()=> { return { aa:bb } }
  	state:()=>({
      cart: JSON.parse(uni.getStorageSync('cart') || [])
    }),
    mutations: { 
        //加入购物车
        addToCart(state,goods){
             const findResult = state.cart.find( (x) => x.goods_id === goods.goods_id)
             if(!findResult){
               state.cart.push(goods)
             }else{
               findResult.goods_count++
             }
             uni.setStorageSync('cart',JSON.stringify(state.cart))
        },
        updateGoodsState(state,goods){
             const findResult = state.cart.find( (x) => x.goods_id === goods.goods_id)
             if(findResult){
               findResult.goods_state = goods.goods_state
               uni.setStorageSync('cart',JSON.stringify(state.cart))
             }
                         
             let c = 0;
             state.cart.forEach( (goods)=>{c+= goods.goods_count})
             
             uni.setTabBarBadge({
               index: 2,
               text: c+''
             })
             
             setTimeout(()=>{
               goods.callback && goods.callback()
             },300)
        },
        updateGoodsCount(state,goods){
          const findResult = state.cart.find( (x) => x.goods_id === goods.goods_id)
            if(findResult) {
              // 更新对应商品的数量
              findResult.goods_count = goods.goods_count
              findResult.goods_state = 1
              // 持久化存储到本地
              uni.setStorageSync('cart',JSON.stringify(state.cart))
            }
             
            let c = 0;
            state.cart.forEach( (goods)=>{c+= goods.goods_count})
         
            uni.setTabBarBadge({
              index: 2,
              text: c+''
            })
            
            setTimeout(()=>{
              goods.callback && goods.callback()
            },300)
        },
        removeGoodsById(state,key){
            state.cart.splice(key,1)
            uni.setStorageSync('cart',JSON.stringify(state.cart))
            
            let c = 0;
            state.cart.forEach( (goods)=>{c+= goods.goods_count})
            
            uni.setTabBarBadge({
              index: 2,
              text: c+''
            })
            
        },
        checkAll(state,check){
            state.cart.forEach( (item) => {item.goods_state = check})
            uni.setStorageSync('cart',JSON.stringify(state.cart))
        }
       
    },
    
    //类似store的计算属性
    getters: { 
        // 统计购物车中商品的总数量
         total(state) {
            let c = 0;
            state.cart.forEach( (goods)=>{c+= goods.goods_count})
            return c
         },
         checkedCount(state){
           return state.cart.filter(x => x.goods_state).reduce((total, item) => total += item.goods_count, 0)
         },
        checkedGoodsAmount(state) {
          // 先使用 filter 方法，从购物车中过滤器已勾选的商品
          // 再使用 reduce 方法，将已勾选的商品数量 * 单价之后，进行累加
          // reduce() 的返回值就是已勾选的商品的总价
          // 最后调用 toFixed(2) 方法，保留两位小数
          return state.cart.filter(x => x.goods_state)
                           .reduce((total, item) => total += item.goods_count * item.goods_price, 0)
                           .toFixed(2)
        }
    },
    
}
 
 
 
//用户信息模块
const moduleUser = {
   state: ()=> ({
     address: JSON.parse(uni.getStorageSync('address') || '{}'),
   }),
   
   mutations: {
     updateAddress(state,address){
       state.address = address
       uni.setStorageSync('address', JSON.stringify(state.address))
     }
   },
   
   //重新组织地址
   getters:{
     addstr(state){
       return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.streetName + state.address.detailInfoNew
     }
   },
   
}
 
 
 
const store = createStore({
    modules: {
       m_cart : moduleCart,
       m_user: moduleUser
    }
})

export default store